(function() {
	require(['jquery', 'box'], function(jquery, box) {
		
		/*工程名称下拉列表*/
		var $eventSelect2 = null;
		$.ajax({
			url: 'projectCotegory/retieveProjectCotegorys.service',
			type: 'post',
			success: function(data) {
				var _data = data.data;
				var select2Data = $.map(_data, function (obj) {
					obj.text = obj.name; 
					obj.id = obj.id;
					return obj;
				});
				$eventSelect2 = $("#create_project").select2({
					placeholder: "工程名称",
					allowClear: true,
					data: select2Data,
					minimumResultsForSearch: Infinity
				}).val("test_select2_null").trigger("change");
			}
		});
		
		$('#create_project').change(function() {
			var pcid = $("#create_project").val();
			/*目标属性下拉列表*/
			var $eventSelect1 = null;
			$.ajax({
				url: 'targetAttribute/retrieveTargetAttributeBypcid.service',
				type: 'post',
				data: {
					pcid: pcid
				},
				success: function(data) {
					var _data = data.data;
					var select2Data = $.map(_data, function (obj) {
						  obj.text = obj.text || obj.targetAttribute; 
						  obj.id = obj.id;
						  return obj;
						});
					$eventSelect1 = $("#create_attribute").select2({
						 placeholder: "项目名称",
						 allowClear: true,
						 data: select2Data,
						 minimumResultsForSearch: Infinity
					 }).val("test_select2_null").trigger("change");
				}
			});
		});
		
		$('#provinceTarget').bind('click', function(evt) {
			var t = evt.target, tagName = t.tagName, defaultTag = "I",
				$t = {}, cls = "", addFlag = "plus", deleteFlag = "minus",
				annualTarget = "aunnal-target", flag = -1,
				//添加时用
				boxesDiv = {}, boxes = [], boxesLen = 0, $boxes = {},
				//删除时用
				acBox= {}, dataIndex = 0, $acBox = {}, temp = "";
			
			//如果不是I标签，不做处理
			if(tagName != defaultTag) return;
			
			$t = $(t);
			cls = $t.attr("class");

			if(cls.indexOf(addFlag)> flag) {
				if(cls.indexOf(annualTarget)> flag) {
					$('#' + modalBodyId).html('请填写年度指标，再添加！！');
					
					boxes = $t.parent().next().children();
					boxesLen = boxes.length;
					temp = $(boxes[boxesLen-1]).find('input[name=annualTargets]').val();
					temp? $(boxes[boxesLen-1]).after(targetAunnalBox(boxesLen+1)): tip_modal.modal();return;
				} else {
					boxesDiv = $t.parent().next().children()[0];
					boxes = boxesDiv.children;
					boxesLen = boxes.length;
					//添加指标的操作
					if(inputIsDone(boxes[boxesLen-1])) {
						addFun($(boxesDiv), targetBox(boxesLen+1));
					} else {
						$.alert({title: '提示', content: "请先填写当前目标或指标！"});
						return;
					}
				}
				
			} else if(cls.indexOf(deleteFlag)> flag) {
				acBox = $t.parent();
				$acBox = $(acBox);
				dataIndex = $acBox.attr('data-index');
				boxes = $acBox.nextAll();
					
				//重新排序
				if(boxes.length != 0 && cls.indexOf(annualTarget)> flag) {
					reSortAunnalTarget(boxes, dataIndex);
				} else {
					reSort(boxes, dataIndex);
				}
				//删除指标的操做
				deleteFun($acBox);
			} else {
				return;
			}
			
			//需要添加的指标单元
			function targetBox(index) {
				return '<div class="ac_box top-border" data-index="' + index + '">' + '\n'
			   			+ '<label class="lr-4 span-add indexName">指标' + index + ':</label>' + '\n'
			   			+ '<input class= "fill-3" name="thirdIndex"></input>' + '\n'
			   			+ '<label class="lr-4 span-add">指标值</label>' + '\n'
			   			+ '<input class= "fill-4" name="indexValue"></input>' + '\n'
			   			+ '<i class="fa fa-fw fa-minus-square-o" title="删除数量指标' + index + '" data-add="minus"></i>' + '\n'
			   		+ '</div>';
			}
			
			//需要添加的年度目标
			function targetAunnalBox(index) {
				return '<div class="aunnalTargetBox top-border" data-index="' + index + '">' + '\n'
						+ '<label class="lr-4 indexName">目标' + index + ':</label>' + '\n'
						+ '<input class= "fill-1" name="annualTargets"></input>' + '\n'
						+ '<i class="fa fa-fw fa-minus-square-o aunnal-target" title="删除年度目标' + index + '" data-add="minus"></i>' + '\n'
						+ '</div>';
			}
			
			//判断指标的两个填报框：指标名，指标值
			//如果都有值，返回true；
			//其他情况返回false。
			function inputIsDone(obj) {
				var inputCol = $(obj).find('input'), i=0,
				inputColLen = inputCol.length, ta = null, valuesLen = 0;
				for(i=0; i<inputColLen; i++) {
					ta = inputCol[i];
					$.trim($(ta).val())? (valuesLen++) : 0;
				}
				//如果一个指标没有填满指标名和指标值（共两个）不能添加指标
				if(valuesLen == 2) {
					return true;
				} else {
					return false;
				}
			}
			
			//在给定的对象parentObj上，添加addObj对象
			function addFun(parentObj, addObj) {
				parentObj.append(addObj);
			}
			
			function reSort(objs, startIndex) {
				var len = objs.length, i=0, obj = {}, $obj = {}, newIndex = 0;
				for(i=0; i<len; i++) {
					obj = objs[i];
					$obj = $(obj);
					newIndex = parseInt(startIndex)+i;
					$obj.attr('data-index', newIndex);
					$obj.find('.indexName').html('指标' + newIndex + ':');
					$obj.find('i').attr('title', "删除指标" + newIndex + ':');
				}
			}
			
			function reSortAunnalTarget(objs, startIndex) {
				var len = objs.length, i=0, obj = {}, $obj = {}, newIndex = 0;
				for(i=0; i<len; i++) {
					obj = objs[i];
					$obj = $(obj);
					newIndex = parseInt(startIndex)+i;
					$obj.attr('data-index', newIndex);
					$obj.find('.indexName').html('指标' + newIndex + ':');
					$obj.find('i').attr('title', "删除年度目标" + newIndex + ':');
				}
			}
			
			function deleteFun(obj) {
				obj.remove();
			}
			
			
		});
		
	});
})();